package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     *  新增套餐
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 设置状态默认值
        setmeal.setStatus(StatusConstant.ENABLE);
        // 调用mapper层添加套餐
        this.setmealMapper.save(setmeal);

        // 拿到回调的套餐id
        Long id = setmeal.getId();

        // 获取套餐菜品关联信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (CollectionUtils.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
            // 调用Mapper层添加套餐菜品关联信息
            this.setmealDishMapper.save(setmealDishes);
        }
    }

    /**
     *  套餐分页查询
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 启动分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        // 调用mapper获取数据
        Page<SetmealVO> page = this.setmealMapper.pageQuery(setmealPageQueryDTO.getName(),setmealPageQueryDTO.getCategoryId(),setmealPageQueryDTO.getStatus());
        // 返回结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     *  套餐批量删除
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        // 1.查看套餐是否是启售状态，启售状态不能被删除
        Integer startCount = this.setmealMapper.getCountByIds(ids);
        if(startCount > 0){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        // 删除套餐
        this.setmealMapper.deleteSetmeal(ids);
        // 删除套餐菜品关联信息
        this.setmealDishMapper.delete(ids);
    }

    /**
     *  根据套餐id查询套餐信息
     */
    @Transactional
    @Override
    public SetmealVO getSetmealById(Long id) {
        // 调用mapper获取套餐信息
        Setmeal setmeal = this.setmealMapper.getSetmealById(id);
        // 获取套餐关联的菜品信息
        List<SetmealDish> setmealDishes = this.setmealDishMapper.getDishBySetmealIds(id);
        // 创建一个SetmealVO返回对象
        SetmealVO setmealVO = new SetmealVO();
        // 将查询到的结果封装到返回对象SetmealVO中
        BeanUtils.copyProperties(setmeal,setmealVO);
        // 将菜品信息封装到返回对象SetmealVO中
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     *  修改套餐信息
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        // 创建Setmeal对象用来储存将要修改后的套餐对象
        Setmeal setmeal = new Setmeal();
        // 将对象信息copy给新的对象
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 调用mapper层进行套餐信息修改
        this.setmealMapper.update(setmeal);
        // 删除原来套餐菜品关联信息
        this.setmealDishMapper.delete(Arrays.asList(setmealDTO.getId()));
        // 获取修改后的套餐菜品关联信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 判断是否有新的菜品要添加到套餐中
        if(CollectionUtils.isNotEmpty(setmealDishes)){
            // 给套餐菜品关联信息添加套餐id
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            // 调用Mapper层添加套餐菜品关联信息
            this.setmealDishMapper.save(setmealDishes);
        }
    }

    /**
     *  套餐启售停售
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 判断套餐是否是启售操作
        if(status == StatusConstant.ENABLE){
            // 套餐启售
            // 获取套餐包含的菜品id
            List<Long> dishes = this.setmealDishMapper.getDishIdsBySetmealId(id);
            if(CollectionUtils.isNotEmpty(dishes)) {
                // 根据菜品id查询是否为停售状态
                Integer startCount = this.dishMapper.getStopCountByIds(dishes);
                // 判断套餐中是否有启售的菜品
                if (startCount > 0) {
                    // 有启售的菜品
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        this.setmealMapper.update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
